-- slime_area_damage
-- create by zhuangsl
-- 技能：范围伤害。以目标为中心，对周围n * n范围敌人造成伤害
-- 目前支持如下配置：
-- 1、slime_area_damage(5, 1000, 500,1)：5为slime_skill_id，对半径1范围内的敌人造成威力 1000+500*level 的伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            local prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        local base = para[2];
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);
        
        -- 斯巴达专属加成
        local prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end
        damage = PropM.apply(prop, damage);

        -- 遍历目标周围所有已开且存活的怪
        local radius = para[4];
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local c = DungeonM.convertToCoor(p);

            -- 有效范围内
            local near = math.abs(c.x - coor.x) <= radius and math.abs(c.y - coor.y) <= radius;

            local grid = DungeonM.getGridByPos(p);

            -- 翻开且存活的怪物
            if  near and grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        for _, monster in pairs(ret) do
            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(monster):hit(source, monster, skillId);

            -- 受创
            CombatM.receiveDamage(source, monster, damage, skillId, extra);
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4])};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        local base = para[2];
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);
        
        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{damage}", damage);

        return desc;
    end,
};
